#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         DateTimeEditlet.py
#
# Copyright:    Copyright (c) 2005, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the DateTimeEditlet class, that is
#               used to generate date/time editlets for property editors.
#
#               It's implementation is based on the LGPL DHTML
#               DateTimeChooser By DynArch.
#               (http://www.dynarch.com/projects/calendar/)
#
#               It uses the Zope DateTime to represent dates and time.
#
# See Also:     PropertyEditlet, PropertyRefleciton, 
#               PropertyDefinition, PropertyAccess 
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Editlet import Editlet
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *

#####################################################################
## Zope Library Imports
from DateTime import DateTime
 
#####################################################################
## Python Library Imports
# (none)

#####################################################################
## DateTimeEditlet Class Definition
class DateTimeEditlet(Editlet):
    """
        An editlet for editing date time properties  
    """
    
    #---------------------------------------------------------------#
    
    def isEditable(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns if the specified propertyDefinition can be
            edited by this editlet
        """

        return propertyDefinition.getType() in [pt_DATE] and propertyDefinition.getStructure() == ps_ATOMIC and propertyDefinition.getFormat() == pf_NATIVE
        
    #---------------------------------------------------------------#

    def generateEditorFor(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns a HTML editlet for the specified property.
        """

        #the resulting editlet
        result = ""
        
        #get the value for the property
        if isForConstructor:
            value = propertyDefinition.getDefaultValue()
        else:
            #get the value for the property
            value = propertyAccess.getPropertyValue(propertyDefinition.getIdentity())                                                    

        #is the default None? if so we use the current date
        if value is None:
            value = DateTime()

        #create the editor
        result = result + "<span>"
        result = result + "<input type='text' style='margin-bottom: 1em;' name=\"ke%s\" id=\"ke%s\" size=\"%s\" value=\"%s %d, %d\" readonly='true'> " % (propertyDefinition.getIdentity(), propertyDefinition.getIdentity(), 20, value.aMonth(), value.day(), value.year())           
        result = result + "<input type='button' style='margin-bottom: 1em;cursor: pointer;' id=\"ke%sbutton\" value='...'/>" % propertyDefinition.getIdentity()
        result = result + "</span>"        
        result = result + "<script type='text/javascript'>Calendar.setup({inputField:'ke%s', ifFormat:'%%b %%e, %%Y', button:'ke%sbutton', weekNumbers:false});</script>" % (propertyDefinition.getIdentity(), propertyDefinition.getIdentity())

        return result
        
    #---------------------------------------------------------------#

    def updatePropertyWith(self, propertyDefinition, propertyAccess, REQUEST=None):
        """
            Attempts to update the specified property on the
            object implementing the PropertyAccess interface with the
            specified HTTP REQUEST.
        """

        #is write permitted on the specified property?
        if propertyDefinition.isWritePermitted():

            # Check we have a value if it isMandatory
            propertyDefinition.verifyMandatoryValue(DateTime(REQUEST["ke%s" % (propertyDefinition.getIdentity(),)]))
        
            #set the value for the property from the REQUEST
            propertyAccess.setPropertyValue(propertyDefinition.getIdentity(), DateTime(REQUEST["ke%s" % (propertyDefinition.getIdentity(),)]))
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

# Register Editlet Information with the Knowledge Engine
registerEditlet("DateTimeEditlet", DateTimeEditlet())

#####################################################################
